---------------------------------------------------------------
-- @Description：         game_actions
---------------------------------------------------------------
local skynet = require "skynet"
local log = require "sky_log"
local CMD = require "game_cmds"
local packetetbase = require "lua_packetbase"
local socket = require "skynet.socket"
local cc = require "sky_call"
local uu = require "sky_utils"
local gg = require "game_utils"
local common = require "handle_common"
require "def"

local handle_call_score = require "handle_call_score"
local handle_first = require "handle_first"
local handle_second = require "handle_second"
local handle_third = require "handle_third"
local handle_fourth = require "handle_fourth"

--塔尼布牌局桌子状态
local GAME_STATE = {
    GAME_TARNEEB_STATE_FREE             =   0, --空闲阶段
    GAME_TARNEEB_STATE_READY            =   1, --准备阶段
    GAME_TARNEEB_STATE_CALL_GOALS       =   2, --叫分
    GAME_TARNEEB_STATE_CHIOCE_COLOR     =   3, --定花色
    GAME_TARNEEB_STATE_DEARL_CARD       =   4, --出牌
    GAME_TARNEEB_STATE_ROUND_OVER       =   5, --一轮结束
    GAME_TARNEEB_STATE_ROUND_OVERTIMER  =   6, --定时开始游戏状态
    GAME_TARNEEB_STATE_GAME_OVER        =   7, --游戏结束状态
}

local M = {
    --最大人数  
    MAX_PLAYER = 4
}

local function send_msg(fd, msg, size)
    --[[
    log.info("++++++++++++++++++++++++++++++++")
    log.info("robot_logic send_msg fd=", fd)
    log.info("robot_logic send_msg msg=", msg)
    log.info("robot_logic send_msg size=", size)
    log.info("++++++++++++++++++++++++++++++++")
    --]]

    socket.write(fd, msg, size)
end

--该文件调用的robot数据汇总
function M.init(robot)
    --log.warning("robot_tarneeb init! robot=", robot)

    --已叫分标志位
    robot.call_score = nil
    --手牌源数据
    robot.handcards = nil
    --手牌源数据转换成易读手牌表
    robot.read_handcards = nil
    --桌子叫分记录表
    robot.call_score_t = nil
    --机器人计算自己得到的叫分数据
    robot.calc_call_score_t = nil
    --叫分数据
    robot.call_score_seatid  = nil
    robot.call_score_goals = nil
    --本轮定的主花色
    robot.main_color = nil
    --当前是第轮
    robot.current_round = 1
    --本回合第一个玩家出的花色
    robot.round_color = nil
    --对手无花色记录表
    robot.enemy_no_color_t = {}
    --队友无花色记录表
    robot.friend_no_color_t = {}
    --出牌历史表
    robot.outcard_history = {}
    --出牌历史详细表 （包含出牌者，出牌回合）
    robot.outcard_history_more = {}
end

--============================= actions =============================--
local function delay_times(n_time)
    local delay_times_n = tonumber(skynet.getenv("delay_times_n"))
    if not delay_times_n
    or delay_times_n < 1 then
        return
    end

    if not n_time then
        n_time = math.random(60, 200)
    end

    skynet.sleep(n_time)
end

local function on_deal_card_tarneeb(robot, fd, packet)
    local seatid = packet:read_byte()
    local total_cardnum = packet:read_short()

    if not seatid 
    or not total_cardnum then 
        log.error("on_deal_card_tarneeb error! seatid=",  (seatid or "nil")
            .." total_cardnum="..(total_cardnum or "nil"))
        return
    end

    if tonumber(seatid)~=tonumber(robot.m_seatid) then 
        log.error("on_deal_card_tarneeb error2! seatid=",  (seatid or "nil")
            .." m_seatid="..(robot.m_seatid or "nil"))
        return
    end

    local index = math.random(1, 100)
    if index < 13 then 
        local id = math.random(1, 15)
        common.do_send_expression(fd, robot.tid, robot.m_seatid, id)
    end

    local handcards_t = {}
    for i=1, total_cardnum do
        local card = packet:read_short()
        table.insert(handcards_t, card)
    end
    table.sort(handcards_t, function(a,b) return a<b end)

    local read_handcards_t = {}
    for _, v in ipairs(handcards_t) do
        local index = string.format("%04X", v)
        if index then
            read_handcards_t[v] = {
                value = index,
                trans = PUKE_CARDS[v],
            }
        end
    end

    robot.handcards = handcards_t
    robot.read_handcards = read_handcards_t

    --local debug_card_info = {}
    --for _, v in pairs(robot.read_handcards) do
    --    table.insert(debug_card_info, v.trans)
    --end
    --log.info("on_deal_card robot.read_handcards=", debug_card_info)
end

local function on_broadcast_newround_start_tarneeb(robot, fd, packet)
    local table_state = packet:read_byte()
    local cur_round = packet:read_int()
    local dealer = packet:read_byte()

    if tonumber(dealer)~=tonumber(robot.m_seatid) then 
        return
    end
    --log.info("on_broadcast_newround_start_tarneeb=", robot)

    --首位叫分逻辑
    local call_type = 1         --叫分类型: 0.叫分 1.放弃叫分
    local call_score, config_index, all_color_score, main_color
    --已经叫过了的直接pass
    if robot.call_score~=true then 
        local pos = gg.get_call_score_pos(robot.call_score_t)
        local seat_info = {}
        seat_info.m_seatid = robot.m_seatid
        seat_info.m_userid = robot.user_id
        seat_info.tid = robot.tid
        seat_info.detail = {}
        for seat_id=1, M.MAX_PLAYER do
            local player_info = {}
            player_info.user_id = robot.table_info.players[seat_id].user_id
            player_info.is_real_player = robot.table_info.players[seat_id].is_real_player
            player_info.seatid = robot.table_info.players[seat_id].seatid
            player_info.chips = robot.table_info.players[seat_id].chips
            seat_info.detail[seat_id] = player_info
        end

        call_score, config_index, all_color_score, main_color = handle_call_score.calc_handcards_socre(robot.handcards, 
            pos, robot.call_score_t or {}, seat_info, robot.platid)
        if call_score and call_score~=0 then 
            call_type = 0
        end
        robot.call_score = true

        robot.calc_call_score_t = {
            call_score = call_score,
            config_index = config_index,
            all_color_score = all_color_score,
            pos = pos,
            main_color = main_color,
        }
    end

    delay_times()

    local pack = packetetbase.create_writebase()
    pack:write_begin(CMD.CLIENT_COMMAND_TARNEEB_USER_CALL_GOALS)
    --叫分类型: 0.叫分 1.放弃叫分
    pack:write_byte(call_type)
    --叫的分数   
    pack:write_int(call_score or 0)
    pack:write_byte(config_index)
    pack:write_float64(all_color_score)
    pack:write_end()
    
    local msg, size = pack:return_buf()
    send_msg(fd, msg, size)

    --log.info("on_broadcast_newround_start_tarneeb robot=", robot)
end

local function on_broadcast_call_goals_tarneeb(robot, fd, packet)
    if not robot
    or not robot.m_seatid then 
        return
    end

    local _type = packet:read_byte()     --叫分类型: 1.放弃叫分 0.参与叫分
    local err = packet:read_byte() 
    local seatid = packet:read_byte() 
    local score = packet:read_int() 

    local t = {
        type = tonumber(_type),
        err = err,
        seatid = tonumber(seatid),
        score = tonumber(score),
    }
    
    if not robot.call_score_t then
        robot.call_score_t = {}
    end
    table.insert(robot.call_score_t, t)
    --log.info("on_broadcast_call_goals_tarneeb robot.call_score_t=", robot.call_score_t)
    --[[
    log.info("on_broadcast_call_goals_tarneeb type=", _type)
    log.info("on_broadcast_call_goals_tarneeb err=", err)
    log.info("on_broadcast_call_goals_tarneeb seatid=", seatid)
    log.info("on_broadcast_call_goals_tarneeb score=", score)
    --]]
end

local function on_next_user_call_goals_tarneeb(robot, fd, packet)
    local seatid = tonumber(packet:read_byte()) 
    if tonumber(seatid)~=tonumber(robot.m_seatid) then 
        return
    end

    local call_type = 1         --叫分类型: 0.叫分 1.放弃叫分
    local call_score, config_index, all_color_score, main_color


    local pos = gg.get_call_score_pos(robot.call_score_t)
    local seat_info = {}
    seat_info.m_seatid = robot.m_seatid
    seat_info.m_userid = robot.user_id
    seat_info.tid = robot.tid
    seat_info.detail = {}
    for seat_id=1, M.MAX_PLAYER do
        local player_info = {}
        player_info.user_id = robot.table_info.players[seat_id].user_id
        player_info.is_real_player = robot.table_info.players[seat_id].is_real_player
        player_info.seatid = robot.table_info.players[seat_id].seat_id
        player_info.chips = robot.table_info.players[seat_id].chips
        seat_info.detail[seat_id] = player_info
    end

    call_score, config_index, all_color_score, main_color = handle_call_score.calc_handcards_socre(robot.handcards, 
        pos, robot.call_score_t, seat_info)
    if call_score~=0 then 
        call_type = 0
    end
    robot.call_score = true

    robot.calc_call_score_t = {
        call_type = call_type,
        call_score = call_score,
        config_index = config_index,
        all_color_score = all_color_score,
        pos = pos,
        main_color = main_color,
    }
    
    --小于前面的叫分则pass
    if tonumber(call_score or 0) <= tonumber(robot.call_score_t[#robot.call_score_t].score) then 
        call_type = 1
    end

    --13分特殊处理：前面没有到10分不能直接叫13分
    if tonumber(call_score)==13 then 
        if tonumber(robot.call_score_t[1].score) < 10 then 
            call_score = tonumber(robot.call_score_t[1].score) + 1
        end
    end

    delay_times()

    local pack = packetetbase.create_writebase()
    pack:write_begin(CMD.CLIENT_COMMAND_TARNEEB_USER_CALL_GOALS)
    pack:write_byte(call_type or 1)                  --叫分类型
    pack:write_int(call_score or 0)                  --叫的分数 
    pack:write_byte(config_index or 0)               --用的第几号数值表
    pack:write_float64(all_color_score or 0)         --牌型总分
    pack:write_end()
    
    local msg, size = pack:return_buf()
    send_msg(fd, msg, size)

    --log.info("on_next_user_call_goals_tarneeb robot=", robot)
end

local function on_broadcast_call_goals_over(robot, fd, packet)
    if not robot
    or not robot.m_seatid then
        return
    end


    --叫分成功的玩家椅子id
    local call_score_seatid = packet:read_byte()
    --叫分成功的玩家叫的分数
    local call_score_goals = packet:read_byte() 

    robot.call_score_seatid = call_score_seatid
    robot.call_score_goals = call_score_goals

    --log.info("on_broadcast_call_goals_over")
end

local function on_user_choice_color_tarneeb(robot, fd, packet)
   
    local seatid = packet:read_byte()   
    local state = packet:read_byte() 

    if tonumber(seatid)~=tonumber(robot.m_seatid) then 
        return
    end

    --log.info("on_broadcast_choice_color_over_tarneeb robot=", robot)

    local main_color = robot.calc_call_score_t.main_color

    delay_times()

    local pack = packetetbase.create_writebase()
    pack:write_begin(CMD.CLIENT_COMMAND_TARNEEB_USER_SELECT_COLOR)
    pack:write_byte(main_color)      
    pack:write_end()
    local msg, size = pack:return_buf()
    send_msg(fd, msg, size)
end

local function on_broadcast_choice_color_over_tarneeb(robot, fd, packet)
    if not robot
    or not robot.m_seatid then 
        return
    end

    --叫分最高的座位号
    local seatid = packet:read_byte()                   
    --主花色 1 2 3 4 对应 0x0100(方) 0x0200(梅) 0x0300(红) 0x0400(黑)
    local main_color = packet:read_byte()

    robot.main_color = main_color

    if seatid==robot.m_seatid then
        log.debug("on_broadcast_choice_color_over_tarneeb: ","tid="..(robot.tid or "nil")
            .." seatid="..(seatid or "nil")
            .." main_color="..((main_color or "nil")) )
    end
end

local function on_broadcast_begin_play_card(robot, fd, packet)
    local seatid = packet:read_byte()                    --第一个打牌玩家的座位
    local current_round = packet:read_byte()             --游戏开始的局数
    local state = packet:read_byte()                     --游戏状态

    if tonumber(seatid)~=tonumber(robot.m_seatid) then 
        return
    end

    --是否叫分队
    local is_call_score_team = uu.check_is_friend(robot.call_score_seatid, robot.m_seatid)
    if tonumber(robot.call_score_seatid)==tonumber(robot.m_seatid) then
        is_call_score_team = true
    end
    --上一回合第一个出牌的椅子号
    local last_round_first_outcard_seatid = gg.get_last_round_first_outcard_seatid(robot.outcard_history_more)
    --玩家是否是上回合第一个出牌的
    local is_first_out_last_round = (last_round_first_outcard_seatid == robot.m_seatid)
    --得到上一回合自己出的牌
    local last_round_outcard = gg.get_last_round_self_outcard(robot.outcard_history_more, robot.m_seatid)

    local card = handle_first.do_card(robot.handcards, is_call_score_team, robot.main_color,
        robot.outcard_history, robot.enemy_no_color_t, robot.friend_no_color_t, robot.current_round,
        is_first_out_last_round, last_round_outcard, robot.outcard_history_more, robot.m_seatid)

    log.debug("[begin pos_1] robot play_card:","tid="..(robot.tid or "nil")
        .." user_id="..(robot.user_id or "nil").." seatid="..(seatid or "nil").." is_call_score_team="..tostring(is_call_score_team)
        .." card="..((robot.read_handcards[card] or {}).value or "nil").." trans="..((robot.read_handcards[card] or {}).trans or "nil")) 

    gg.handcards_remove_card(robot.handcards, robot.read_handcards, card)

    delay_times()

    local pack = packetetbase.create_writebase()
    pack:write_begin(CMD.CLIENT_COMMAND_TARNEEB_USER_PLAY_CARDS)
    pack:write_short(tonumber(card))
    pack:write_end()
    local msg, size = pack:return_buf()
    send_msg(fd, msg, size)
end

local function on_broadcast_play_card_overcome(robot, fd, packet)
    if not robot
    or not robot.m_seatid then 
        return
    end

   -- log.info("robot=", robot)
    local err = packet:read_byte() 
    local seatid = packet:read_byte()           --打牌的座位
    local user_play_card = packet:read_short()  --打出的牌

    if not robot.outcard_history then
        --log.error("robot==nil!!", robot)
        log.error("seatid=", seatid)
        log.info("on_broadcast_play_card_overcome robot.agent=", robot.agent)
        log.info("on_broadcast_play_card_overcome robot.m_seatid=", robot.m_seatid)
    end

    if tonumber(err)~=0 then
        return
    end

    table.insert(robot.outcard_history, user_play_card)
    local t = {
        seatid = seatid,
        outcard = user_play_card,
    }

    local the_color = uu.calc_card_color(user_play_card)

    --是否检测玩家没有某种花色
    local check_flag = true
    --手牌详细数据记录
    local round = #robot.outcard_history_more
    --第一回合第一位玩家出牌
    if not robot.outcard_history_more[round] then 
        check_flag = false
        round = 1
        robot.outcard_history_more[round] = {}
        --每回合第一个玩家出牌时记录花色
        robot.round_color = the_color
    end

    --每回合固定只会记录4次，过了4次就一定是下一回合的出牌了
    if #(robot.outcard_history_more[round])==4 then 
        check_flag = false
        round = round + 1
        robot.outcard_history_more[round] = {}
        --每回合第一个玩家出牌时记录花色
        robot.round_color = the_color
        robot.current_round = robot.current_round + 1
    end
    table.insert(robot.outcard_history_more[round], t)

    local color = uu.calc_card_color(user_play_card)
    --无花色记录
    if check_flag==true then 
        --本次出牌和每回合第一张出牌花色对比即可知道其是否还有该种花色
        local round_color = uu.calc_card_color(robot.outcard_history_more[round][1].outcard)
        if color~=round_color then
            if uu.check_is_friend(robot.m_seatid, seatid) then
                robot.friend_no_color_t[round_color] = true
            else
                if seatid~=robot.m_seatid then
                    robot.enemy_no_color_t[round_color] = true
                end
            end
        end
    end

    local left_n = uu.history_check_color_left_cards_count(robot.handcards, color, robot.outcard_history)
    if left_n==0 then
        robot.friend_no_color_t[color] = true
        robot.enemy_no_color_t[color] = true
    end

    --[[
    log.info("on_broadcast_play_card_overcome err=", err)
    log.info("on_broadcast_play_card_overcome seatid=", seatid)
    log.info("on_broadcast_play_card_overcome user_play_card=", user_play_card)
    --]]
end

local function on_broadcast_next_play_card(robot, fd, packet)
    local seatid = packet:read_byte() 

    if tonumber(seatid)~=tonumber(robot.m_seatid) then 
        return
    end

    --第几位出牌
    local pos = gg.calc_round_pos(robot.outcard_history_more)
    --是否叫分队
    local is_call_score_team = uu.check_is_friend(robot.call_score_seatid, robot.m_seatid)
    if tonumber(robot.call_score_seatid)==tonumber(robot.m_seatid) then
        is_call_score_team = true
    end
    --上一回合第一个出牌的椅子号
    local last_round_first_outcard_seatid = gg.get_last_round_first_outcard_seatid(robot.outcard_history_more)
    --玩家是否是上回合第一个出牌的
    local is_first_out_last_round = (last_round_first_outcard_seatid == robot.m_seatid)
    --得到上一回合自己出的牌
    local last_round_outcard = gg.get_last_round_self_outcard(robot.outcard_history_more, robot.m_seatid)
    --上一玩家出的牌
    local prev_card = gg.get_prev_outcard(robot.outcard_history_more)

    local card
    if pos==1 then 
        card = handle_first.do_card(robot.handcards, is_call_score_team, robot.main_color,
            robot.outcard_history, robot.enemy_no_color_t, robot.friend_no_color_t, robot.current_round,
            is_first_out_last_round, last_round_outcard, robot.outcard_history_more, robot.m_seatid)
    elseif pos==2 then
        card = handle_second.do_card(robot.handcards, robot.main_color, robot.round_color,
            prev_card, robot.outcard_history, robot.enemy_no_color_t, robot.outcard_history_more, robot.m_seatid)
    elseif pos==3 then
        card = handle_third.do_card(robot.handcards, robot.main_color, robot.round_color,
            robot.enemy_no_color_t, robot.outcard_history, robot.outcard_history_more, robot.m_seatid)
    elseif pos==4 then
        card = handle_fourth.do_card(robot.handcards, robot.main_color, robot.round_color,
            robot.enemy_no_color_t, robot.outcard_history, robot.outcard_history_more, robot.m_seatid)
    else
        log.error("get_round_pos error! pos=", pos)
        return 
    end

    local pos_str = '[pos_'..pos..']'
    log.debug(pos_str.." robot play_card:", "tid="..(robot.tid or "nil")
        .." user_id="..(robot.user_id or "nil").." seatid="..(seatid or "nil").." is_call_score_team="..tostring(is_call_score_team)
        .." card="..((robot.read_handcards[card] or {}).value or "nil").." trans="..((robot.read_handcards[card] or {}).trans or "nil")) 

    gg.handcards_remove_card(robot.handcards, robot.read_handcards, card)

    delay_times()

    local pack = packetetbase.create_writebase()
    pack:write_begin(CMD.CLIENT_COMMAND_TARNEEB_USER_PLAY_CARDS)
    pack:write_short(tonumber(card))   
    pack:write_end()
    local msg, size = pack:return_buf()
    send_msg(fd, msg, size)
end

local function on_broadcast_bout_over(robot, fd, packet)
    if not robot
    or not robot.m_seatid then 
        return
    end


    local seat_id = packet:read_byte()              --赢家座位
    local score = packet:read_byte()                --赢家分数

    --[[
    log.info("on_broadcast_bout_over win_seat_id=", seat_id)
    log.info("on_broadcast_bout_over win_score=", score)
    --]]
end

local function on_broadcast_round_over(robot, fd, packet)
    if not robot
    or not robot.m_seatid then 
        return
    end

    --甲队
    local team_1 = packet:read_int()                    --队伍
    local team_1_score = packet:read_int()              --本轮分数
    local team_1_total_score = packet:read_int()        --本局总分

    --乙队
    local team_2 = packet:read_int()                    --队伍
    local team_2_score = packet:read_int()              --本轮分数
    local team_2_total_score = packet:read_int()        --本局总分

    --[[
    log.info("on_broadcast_round_over team_1=", team_1)
    log.info("on_broadcast_round_over team_1_score=", team_1_score)
    log.info("on_broadcast_round_over team_1_total_score=", team_1_total_score)
    log.info("on_broadcast_round_over team_2=", team_2)
    log.info("on_broadcast_round_over team_2_score=", team_2_score)
    log.info("on_broadcast_round_over team_2_total_score=", team_2_total_score)
    --]]
end

local function on_broadcast_game_over(robot, fd, packet)
    if not robot
    or not robot.m_seatid then 
        return
    end

    local win_team = packet:read_int()                  --获胜队伍
    local win_score = packet:read_int()                 --获胜队伍分数

    ---[[
    log.info("on_broadcast_game_over win_team=", win_team)
    log.info("on_broadcast_game_over win_score=", win_score)
    --]]  

    --20%离桌概率
    local index = math.random(1, 100)
    if index <= 20 then
        skynet.send(skynet.self(), "lua", "disconnect_server")
    end
end

local function add_robot_lose_money(platid, add_money)
    local ret, value = cc.call_redis("get", "redis_gi", tonumber(platid), "TARNEEB_ROBOT_TOTAL_LOSE")
    if ret==false then
        log.info("add_robot_lose_money get_money error!")
        return
    end
    value = tonumber(value) or 0
    
    local expired_time
    local now_ts = os.time()
    local last_ts = uu.get_day_last(now_ts)
    local time_diff = skynet.getenv("platform_time_difference")
    --last_ts = last_ts + 3600*(tonumber(time_diff))
    expired_time = last_ts - now_ts
    --log.info("add_robot_lose_money now_ts =", now_ts)
    --log.info("add_robot_lose_money last_ts =", last_ts)
    --log.info("add_robot_lose_money expired_time =", expired_time)
    cc.call_redis("set_robot_total_lose", tonumber(platid), "TARNEEB_ROBOT_TOTAL_LOSE", add_money, expired_time)
end

local function on_broadcast_round_over_info(robot, fd, packet)
    local t = {}
    --199甲队 200乙队
    t.team_1_index = packet:read_int()
    t.team_1_score = packet:read_int()
    t.team_2_index = packet:read_int()
    t.team_2_score = packet:read_int()
    --桌子底注
    t.blind = packet:read_int64()
    --倍数
    t.mutiples = packet:read_int()
    --用于计算的最后得分
    t.last_get_goals = packet:read_int()
    --叫分玩家座位号
    t.call_score_seatid = packet:read_byte()
    --叫分分数
    t.call_score = packet:read_byte()
    --主花色
    t.main_color = packet:read_byte()
    --数据大小
    t.seat_nums = packet:read_int()
    
    t.players = {}
    for i=1, t.seat_nums do
        local player_info = {}
        player_info.seat_id = packet:read_int()
        player_info.uid = packet:read_int64()
        --正数代表赢钱 负数代表输钱
        player_info.win_lose_money = packet:read_int64()
        player_info.real_money = packet:read_int64()
        player_info.score = packet:read_int()
        table.insert(t.players, player_info)
        if player_info.uid==robot.user_id then
            add_robot_lose_money(robot.platid, player_info.win_lose_money)
        end
    end

    if robot and robot.m_seatid then
        --if robot.m_seatid==t.call_score_seatid then
            local infos = {}
            for _, v in pairs(robot.outcard_history_more) do
                for _, _vv in pairs(v) do
                    if (_vv.seatid) then
                        if not infos[_vv.seatid] then
                            infos[_vv.seatid] = {}
                        end 
                        table.insert(infos[_vv.seatid], PUKE_CARDS[_vv.outcard])
                    end
                end
            end
            log.info("outcard_infos=", infos)
        --end

        --每轮结束后的复位
        M.init(robot)
    end

    log.info("on_broadcast_round_over_info :", t)
end

local function on_mandate_play_card(robot, fd, packet)
    local main_color = packet:read_byte()            --当前花色
    local totalBountCount = packet:read_byte()       --总回合数
    --log.info("main_color=", main_color)
    --log.info("totalBountCount=", totalBountCount)

    local tmp_t = {}
    for i=1, totalBountCount do
        for j=1, gg.MAX_PLAYER_TARNEEB do
            local tmp_seatid = packet:read_int()
            local tmp_card = packet:read_short()
            table.insert(tmp_t, {[tmp_seatid]=tmp_card} )
        end 
    end 
    --log.info("tmp_t=", tmp_t)

    --当前回合出牌人数
    local tmp_t2 = {}
    local currentRoundPlayNum = packet:read_int();
    --log.info("currentRoundPlayNum=", currentRoundPlayNum)

    local current_color 
    for i=1, currentRoundPlayNum do
        local tmp_seatid = packet:read_int()
        local tmp_card = packet:read_short()
        table.insert(tmp_t2,  {[tmp_seatid]=tmp_card} )
        if i==1 then
            current_color = uu.calc_card_color(tmp_card)
        end
    end 
    --log.info("tmp_t2=", tmp_t2)

    --当前座位
    local current_seatid = packet:read_byte();   --当前座位
    local currentCardNums = packet:read_short(); --当前手牌数量
    --log.info("currentCardNums=", currentCardNums)
    --log.info("current_seatid=", current_seatid)

    local tmp_t3 = {}
    for i=1, currentCardNums do
        local tmp_card = packet:read_short()
        table.insert(tmp_t3,  tmp_card)
    end 
    --log.info("tmp_t3=", tmp_t3)

    local card
    if gg.check_has_round_color(tmp_t3, current_color) then
        card = uu.get_color_min_card(tmp_t3, current_color)
    else
        local index = math.random(1, #tmp_t3)
        card = table.remove(tmp_t3, index)
    end
    --skynet.sleep(100)

    local pack = packetetbase.create_writebase()
    pack:write_begin(CMD.CLIENT_COMMAND_TARNEEB_ROBOT_MANDATE_PALY_CARD)
    pack:write_short(tonumber(card))
    pack:write_end()
    local msg, size = pack:return_buf()
    send_msg(fd, msg, size)

    log.info("on_mandate_play_card:", "tid="..(robot.tid or "nil")
        .." card="..(string.format("%04X", card or 0) or "nil").." trans="..(PUKE_CARDS[card] or "nil"))
end

--============================= MSG =============================--
M.actions = {
    [CMD.SERVER_COMMAND_TARNEEB_DEAL_CARD] = on_deal_card_tarneeb,
    [CMD.SERVER_TARNEEB_BROADCAST_NEWROUND_START] = on_broadcast_newround_start_tarneeb,
    [CMD.SERVER_TARNEEB_BROADCAST_CALL_GOALS] = on_broadcast_call_goals_tarneeb,
    [CMD.SERVER_TARNEEB_NEXT_USER_CALL_GOALS] = on_next_user_call_goals_tarneeb,
    [CMD.SERVER_TARNEEB_BROADCAST_CALL_GOALS_OVER] = on_broadcast_call_goals_over,
    [CMD.SERVER_TARNEEB_USER_CHOICE_COLOR] = on_user_choice_color_tarneeb,
    [CMD.SERVER_TARNEEB_BROADCAST_CHOICE_COLOR_OVER] = on_broadcast_choice_color_over_tarneeb,
    [CMD.SERVER_BROADCAST_BEGIN_PLAY_CARD] = on_broadcast_begin_play_card,
    [CMD.SERVER_BROADCAST_PLAY_CARD_OVERCOME] = on_broadcast_play_card_overcome,
    [CMD.SERVER_BROADCAST_NEXT_PLAY_CARD] = on_broadcast_next_play_card,
    [CMD.SERVER_BROADCAST_BOUT_OVER] = on_broadcast_bout_over,
    [CMD.SERVER_BROADCAST_ROUND_OVER] = on_broadcast_round_over,
    [CMD.SERVER_BROADCAST_GAME_OVER] = on_broadcast_game_over,
    [CMD.SERVER_TARNEEB_BROADCAST_ROUND_OVER_INFO] = on_broadcast_round_over_info,
    [CMD.SERVER_TARNEEB_BROADCAST_ROBOT_MANDATE] = on_mandate_play_card,
}

return M